export default {
    propData: function () {
        return {
            radioType: '图片模式',
            distinguishList: [],
            distinguishValue: '1',
            httpService: window.config.httpService,
            photoList: [],
            base64Array: [],
            camera: 'http://127.0.0.1:38088/video=stream&camidx=0',
            roate: 0,
            roateDu: 90,
            isBtnLoading: false,
            formContent: {
                input: ''
            },
            showEdit: false,
            address: '',
            isEdit: false, //false,为可用状态，true为不可用
        }
    },
    // 重置设置项
    resetForm() {
        this.radioType = '图片模式'
        // this.distinguishList = []
        this.distinguishValue = '1'
        this.photoList = []
        this.base64Array = [];
        this.formContent.input = ''
        this.showEdit = false
        this.address = ''
    },
    // 拍照
    photograph() {
        this.isEdit = true;
        var data = {
            "filepath": "base64",  //"b"  指定路径  "base64" 不保存本地，返回base64  ""   保存默认路径下     
            "rotate": "0",      // 图像旋转角度，90的整数倍，默认："0"
            "deskew": '0',      // 纠偏（主头有效），参数：0:不纠偏；1:纠偏
            "deskewval": "10",       // 纠偏像素值：正常给0，正数时多裁，负数时少裁
            "camidx": '0',      // 摄像头索引，参数：0:主头；1:副头
            "quality": '5', //图片质量，图片保存本地调用。0:默认质量；1:高质量；2:较高质量；3:中质量；4:较低质量；5:低质量
            "ColorMode": '0', // 色彩模式，图片保存本地时调用。0：彩色 1：灰色 2：黑白 3：白纸印章 4：去背景色(普通文件) 5:去背景色(身份证)
            "bIsPrint1to1": "0",   // 1:1打印有一点点偏差 是适用于身份证卡片之类  1:开启;  0 :关闭
        };
        let that = this;
        this._grabimage(data).then(res => {
            if (res.status == 200 && res.data.code == '0') {
                this.$api.carmeraUpload({ baseCode: res.data.photoBase64 }, resData => {
                    if (resData.code == 200) {
                        that.base64Array.push(res.data.photoBase64)
                        that.photoList.push(resData.data)
                        this.isEdit = false;
                    }
                })
            }
        })
    },
    // 图片合成pdf
    composePhotosPDF() {
        var data = {
            "outputfilepath": this.address,                  // 输出文件保存地址。例：D://res.jpg
            "outputmaxwidth": "3000",                       // 合成后文件最大宽度。例：3000px
            "inputimagetype": "1",                          // 待合成文件类型：0：file；1：base64
            "outputimagetype": "1",                         // 合成后文件类型：0：jpg；1：PDF
            "imagelist": this.base64Array.toString()  // 待合成图片列表。例："D:\\1.jpg,D:\\2.jpg,D:\\3.jpg"
            // "imagelist": "D:\\1.jpg, D:\\2,jpg, D:\\3.jpg"
        };
        this._photoToPdf(data).then(res => {
            if (res.data.code == '0') {
                this.$Message.success('生成成功！');
                this.isEdit = false;
                this.resetForm()
                this.showHide()
                this.closeEdit()
            }
        })
    },
    // 加载主摄像头分辨率
    loadResolvingPower() {
        this._getresolution({ "camidx": "0", "mode": "1" }).then(res => {
            if (res.status == 200 && res.data.code == '0') {
                let fblarray = res.data.data.split("|");
                for (let i = 0; i < fblarray.length; i++) {
                    const element = fblarray[i];
                    let obj = { label: element, value: element }
                    this.distinguishList.push(obj);
                }
            }
        })
    },
    // 打开摄像头
    openCamera() {
        this.camera = 'http://127.0.0.1:38088/video=stream&camidx=0'
        this.$nextTick(() => {
            this.loadResolvingPower()
        })
    },
    // 关闭摄像头
    closeCamera() {
        this._closeCamera({ "camidx": "0" }).then(res => {
            this.camera = null
            this.distinguishList = []
        })
    },
    // 左转
    trunLeft() {
        this.roate = this.roate + this.roateDu;
        let data = { "camidx": '0', "rotate": String(this.roate) }
        this._rotate(data).then((res) => {
            if (this.roate == 360) {
                this.roate = 0
            }
        })
    },
    trunRight() {
        this.roate = this.roate - this.roateDu;
        let data = { "camidx": '0', "rotate": String(this.roate) }
        this._rotate(data).then((res) => {
            if (this.roate == 360) {
                this.roate = 0
            }
        })
    },
    // 删除单一照片
    removePhoto(row, index) {
        this.photoList.splice(index ? index : this.photoList.findIndex(item => item.uuid == row.uuid), 1)
        this.base64Array.splice(index ? index : this.photoList.findIndex(item => item.uuid == row.uuid), 1)
    },
    // 清空列表
    clearAll() {
        this.photoList = []
        this.base64Array = []
    },
    // 分辨率改变触发
    onFbChange(value) {
        let fblArray = value.split("*");
        let that = this;
        this._setresolution({ camidx: "0", mode: "0", width: fblArray[0], height: fblArray[1], }).then(res => {
            if (res.code == '0') {
                that.closeCamera()
                var Gettion = setTimeout(function () {
                    that.openCamera();
                    clearTimeout(Gettion);
                }, 1500);
            }
        })
    },
    apiFunc: function () {
        return {
            // 获取分辨率
            _getresolution(data) {
                return this.http.post("http://127.0.0.1:38088/device=getresolution", JSON.stringify(data))
            },
            // 关闭主摄像头
            _closeCamera(data) {
                return this.http.post("http://127.0.0.1:38088/video=close", JSON.stringify(data))
            },
            _rotate(data) {
                return this.http.post("http://127.0.0.1:38088/video=rotate", JSON.stringify(data))
            },
            _setresolution(data) {
                return this.http.post("http://127.0.0.1:38088/device=setresolution", JSON.stringify(data))
            },
            _grabimage(data) {
                return this.http.post("http://127.0.0.1:38088/video=grabimage", JSON.stringify(data))
            },
            _photoToPdf(data) {
                return this.http.post("http://127.0.0.1:38088/compose=photos", JSON.stringify(data))
            },
        }
    }
}